﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;

namespace Api.WebForm
{
    public class Int32RangeConstraint : IRouteConstraint
    {

        public Int32RangeConstraint(int minValue, int maxValue)
        {
            this.MinValue = minValue;
            this.MaxValue = maxValue;
        }

        private int MinValue;

        private int MaxValue;

        public bool Match(HttpContextBase httpContext, System.Web.Routing.Route route, string parameterName, System.Web.Routing.RouteValueDictionary values, System.Web.Routing.RouteDirection routeDirection)
        {
            try
            {
                if (!values.Any())
                {
                    return false;
                }

                object val = 0;
                if (!values.TryGetValue(parameterName, out val))
                {
                    return false;
                }

                var int_val = Convert.ToInt32(val);

                if (int_val >= this.MinValue && int_val <= this.MaxValue)
                {
                    return true;
                }

                return false;
            }
            catch
            {
                return false;
            }
        }
    }
}